package com.codebo.homiematch.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.codebo.homiematch.common.ErrorCode;
import com.codebo.homiematch.exception.ThrowUtils;
import com.codebo.homiematch.mapper.PostCollectMapper;
import com.codebo.homiematch.mapper.PostLikeMapper;
import com.codebo.homiematch.mapper.PostMapper;
import com.codebo.homiematch.model.dto.PostCreateRequest;
import com.codebo.homiematch.model.dto.PostLikeStarRequest;
import com.codebo.homiematch.model.dto.PostListRequest;
import com.codebo.homiematch.model.entity.Post;
import com.codebo.homiematch.model.entity.PostCollect;
import com.codebo.homiematch.model.entity.PostLike;
import com.codebo.homiematch.model.vo.PostVO;
import com.codebo.homiematch.service.PostCollectService;
import com.codebo.homiematch.service.PostLikeService;
import com.codebo.homiematch.service.PostService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 25912
 * @description 针对表【post(帖子)】的数据库操作Service实现
 * @createDate 2025-07-20 16:36:03
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostMapper, Post>
        implements PostService
{

    @Resource
    private PostLikeMapper postLikeMapper;
    @Resource
    private PostCollectMapper postCollectMapper;
    @Resource
    private PostLikeService postLikeService;
    @Resource
    private PostCollectService postCollectService;

    @Cacheable(value = "postList", key = "'postList'")
    @Override
    public Page<PostVO> getPostList(PostListRequest postListRequest)
    {
        int current = postListRequest.getCurrent();
        int pageSize = postListRequest.getPageSize();
        String sortField = postListRequest.getSortField();
        String sortOrder = postListRequest.getSortOrder();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderBy(sortField != null, !sortOrder.equals("descend"), sortField);

        Page<Post> postPage = this.page(new Page<>(current, pageSize), queryWrapper);

        Page<PostVO> postVOPage = new Page<>(current, pageSize, postPage.getTotal());

        List<PostVO> postVOList = postPage.getRecords().
                stream()
                .map(this::getPostVO)
                .collect(Collectors.toList());
        postVOPage.setRecords(postVOList);

        return postVOPage;
    }

    @Override
    public void createPost(PostCreateRequest postCreateRequest)
    {
        Post post = new Post();
        BeanUtil.copyProperties(postCreateRequest, post);
        post.setTags(JSONUtil.toJsonStr(postCreateRequest.getTags()));
        boolean save = this.save(post);
        ThrowUtils.throwIf(!save, ErrorCode.SYSTEM_ERROR, "创建博客失败");
    }

    @Override
    public PostVO getPost(Long id)
    {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", id);
        Post post = this.getOne(queryWrapper);

        return getPostVO(post);
    }

    @Override
    public void thumb(PostLikeStarRequest postLikeStarRequest)
    {
        PostLike postLike = new PostLike();
        BeanUtil.copyProperties(postLikeStarRequest, postLike);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("post_id", postLikeStarRequest.getPostId());
        queryWrapper.eq("user_id", postLikeStarRequest.getUserId());
        if (postLikeService.getOne(queryWrapper) != null)
        {
            postLikeMapper.delete(queryWrapper);
            return;
        }
        postLikeService.save(postLike);

    }

    @Override
    public void favour(PostLikeStarRequest postLikeStarRequest)
    {
        PostCollect postCollect = new PostCollect();
        BeanUtil.copyProperties(postLikeStarRequest, postCollect);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("post_id", postLikeStarRequest.getPostId());
        queryWrapper.eq("user_id", postLikeStarRequest.getUserId());
        if (postCollectService.getOne(queryWrapper) != null)
        {
            postCollectMapper.delete(queryWrapper);
            return;
        }
        postCollectService.save(postCollect);
    }

    @Override
    public boolean thumbStatus(PostLikeStarRequest postLikeStarRequest)
    {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("post_id", postLikeStarRequest.getPostId());
        queryWrapper.eq("user_id", postLikeStarRequest.getUserId());
        PostLike one = postLikeService.getOne(queryWrapper);
        return one != null;
    }

    @Override
    public boolean favorStatus(PostLikeStarRequest postLikeStarRequest)
    {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("post_id", postLikeStarRequest.getPostId());
        queryWrapper.eq("user_id", postLikeStarRequest.getUserId());
        PostCollect one = postCollectService.getOne(queryWrapper);
        return one != null;
    }

    @Override
    public PostVO getPostVO(Post post)
    {
        PostVO postVO = new PostVO();
        BeanUtil.copyProperties(post, postVO);
        return postVO;
    }
}




